package com.wangzhen.codebase.transfermoney.lock11;

import java.util.ArrayList;
import java.util.List;

public class Allocater {
    private List<Account> list = new ArrayList<>();

    public synchronized void apply(Account src, Account dest) {
        while (list.contains(src) || list.contains(dest)) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try{
            System.out.println("before sleep");
            Thread.sleep(5000);
        } catch (Exception e){

        }
        list.add(src);
        list.add(dest);
    }

    public synchronized void free(Account src, Account dest) {
        if (list.contains(src)) {
            list.remove(src);
        }
        if (list.contains(dest)) {
            list.remove(dest);
        }
        notifyAll();
    }



}
